<template>
  <div class="app-container">
    <div class="head">
      <img src="@/assets/img/Layout/position.png" />
      <p class="see-block">生产计划列表</p>
    </div>
    <div class="view-container">
      <el-form :model="queryParams" ref="queryForm" :inline="true">
        <el-form-item label="批次号" prop="batchNumber">
          <el-input
            v-model="queryParams.batchNumber"
            placeholder="请输入批次号检索"
            clearable
            size="small"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="名称" prop="productName">
          <el-input
            v-model="queryParams.productName"
            placeholder="请输入商品名称检索"
            clearable
            size="small"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="生产计划状态" prop="plunStatus" >
          <el-select
            v-model="queryParams.plunStatus"
            placeholder="全部"
            clearable
            size="small"
            style="width:100px;"
          >
            <el-option
              v-for="item in plunStatus"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="审核状态" prop="approveStatus" >
          <el-select
            v-model="queryParams.approveStatus"
            placeholder="全部"
            clearable
            size="small"
            style="width:100px;"
          >
            <el-option
              v-for="item in approveStatus"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery" class="list-search btn-success" color="#5ED4D4">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery" class="list-search">重置</el-button>
        </el-form-item>
      </el-form>
      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button
            class="btn-success"
            type="primary"
            icon="el-icon-plus"
            size="mini"
            @click="planAdd()"
            v-hasPermi="['system:productPlan:add']"
          >新建</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            v-if="isApprove"
            class="btn-approve"
            type="primary"
            icon="el-icon-edit"
            size="mini"
            @click="planAllApprove()"
            v-hasPermi="['system:productPlan:approve']"
          >批量审核</el-button>
        </el-col>
        <!-- <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar> -->
      </el-row>
      <el-table v-loading="loading" :data="planList" @row-click="handleView" @selection-change="handleSelectionChange" border>
        <el-table-column type="selection" width="35" align="center" />
        <el-table-column label="序号" type="index" align="center" width="50">
           <template slot-scope="scope">
            <span>{{(queryParams.pageNum - 1) * queryParams.pageSize + scope.$index + 1}}</span>
          </template>
        </el-table-column>
        <el-table-column label="计划编码"  align="center" width="150px" prop="planCode" />
        <el-table-column label="产品名称" width="180px"  align="center" prop="productBasicName" :show-overflow-tooltip="true"/>
        <el-table-column label="规格" width="180px"  align="center" prop="specification" :show-overflow-tooltip="true"/>
        <el-table-column label="产品批号" align="center" width="120px" prop="batchNumber"  :show-overflow-tooltip="true" />
        <el-table-column label="箱规" align="center" prop="productSpace" :show-overflow-tooltip="true" />
        <el-table-column label="大码规格" align="center" prop="largeSizeSpecifications" :show-overflow-tooltip="true" />
        <el-table-column label="生产序列号" align="center" width="120px" prop="productionSerialNumber" :show-overflow-tooltip="true" />
        <el-table-column label="计划数量" align="center" prop="planNum" :show-overflow-tooltip="true" />
        <el-table-column label="完成数量" align="center" prop="endNum" :show-overflow-tooltip="true" />
        <el-table-column value-format="yyyy-MM-dd" label="计划开始时间" align="center" prop="planStartTime" width="180px" />
        <el-table-column value-format="yyyy-MM-dd" label="计划结束时间" align="center" prop="planEndTime" width="180px" />
        <el-table-column value-format="yyyy-MM-dd" label="共耗时" align="center" prop="allTime" />
        <el-table-column label="码格式" align="center" prop="codeFormat">
          <template slot-scope="scope">
            <div v-if="scope.row.codeFormat == 1">一物一码</div>
            <div v-if="scope.row.codeFormat == 2">UDI码</div>
          </template>
        </el-table-column>
        <el-table-column label="状态" align="center" prop="plunStatus">
          <template slot-scope="scope">
            <div v-if="scope.row.plunStatus == 0" style="color:#13C2C2">计划中</div>
            <div v-if="scope.row.plunStatus == 1" style="color:#13C2C2">进行中</div>
            <div v-if="scope.row.plunStatus == 2" style="color:#52C41A">已完成</div>
            <div v-if="scope.row.plunStatus == 3" style="color:#13C2C2">草稿</div>
          </template>
        </el-table-column>
        <el-table-column label="审核状态" align="center" prop="approveStatus">
          <template slot-scope="scope">
            <div v-if="scope.row.approveStatus == 0" style="color:#13C2C2">待审核</div>
            <div v-if="scope.row.approveStatus == 1" style="color:#52C41A">已审核</div>
            <div v-if="scope.row.approveStatus == 2" style="color:#fd562c">反审核</div>
            <div v-if="scope.row.approveStatus == 3" style="color:#fd562c">拒绝</div>
          </template>
        </el-table-column>
        <el-table-column label="备注" align="center" prop="remark" :show-overflow-tooltip="true" />
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-delete"
              @click.native.stop="handleDelete(scope.row)"
              v-hasPermi="['system:productPlan:remove']"
            >删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <pagination
        v-show="total > 0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
    </div>
    <!-- 批量审核 -->
    <el-dialog :visible.sync="open" width="500" top="100px" append-to-body title="审核意见" center>
      <el-row>
        <el-col :span="24">
            <textarea
              class="approve-textarea"
              v-model="approveRemark"
              type="textarea"
              placeholder="请输入意见和看法"
            >
            </textarea>
        </el-col>
      </el-row>
      <div slot="footer" class="dialog-footer">
          <el-button
            type="primary"
            class="btn-success mr20 width100"
            @click="agreeApprove()"
          >同意</el-button>
          <el-button
            type="primary"
            class="btn-cancel width100"
            @click="disagreeApprove()"
          >拒绝</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { planList, planAllApprove, planDelete } from "@/api/system/plan/plan";

export default {
  name: "plan",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 总条数
      total: 0,
      // 生产列表数据
      planList: [],
      // 状态
      plunStatus:[
        {
          value:'0',
          label:'计划中'
        },
        {
          value:'1',
          label:'进行中'
        },
        {
          value:'2',
          label:'已完成'
        },
        {
          value:'3',
          label:'草稿'
        }
      ],
      //审核状态
      approveStatus:[
        {
          value:'0',
          label:'待审核'
        },
        {
          value:'1',
          label:'已审核'
        },
        {
          value:'2',
          label:'反审核'
        },
        {
          value:'3',
          label:'拒绝'
        }
      ],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        //产品名称
        productName:undefined,
        //批次号
        batchNumber:undefined,
        //计划状态（0计划中 1进行中 2已完成 3草稿）
        plunStatus: undefined,
        //审核状态（0待审核 1已通过 2 未通过(反审核)3拒绝）
        approveStatus:undefined
      },
      // 审核
      open: false,
      approveRemark: '',
      isApprove: false
    }
  },
  created() {
    this.getList()
  },
  methods: {
    // 获取列表
    getList() {
      this.loading = true
      planList(this.addDateRange(this.queryParams)).then(res => {
          let arr = []
          let that = this
          res.rows.map(item => {
            if(item.planEndTime != null || item.planStartTime != null){
              arr.push(Object.assign(
                {},item,{
                  allTime: that.allTime(item.planEndTime, item.planStartTime) + '天'
                }
              ))
            }else{
              arr.push(Object.assign(
                {},item,{
                  allTime: '暂无'
                }
              ))
            }
          })
          this.planList = arr
          this.total = res.total
          this.loading = false
        }
      )
    },
    // 搜索按钮
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    // 重置按钮
    resetQuery() {
      this.resetForm("queryForm")
      this.handleQuery()
    },
    // 新建
    planAdd(){
      this.$router.push({path:"/production/plan/add"})
    },
    // 批量审核
    planAllApprove(){
      const arr = JSON.parse(JSON.stringify(this.ids))
      if(arr.length > 0){
        // console.log(ids)
        let item = arr.filter((item) => {
          return item.approveStatus === '0'
        })
        let ids = item.map(id => id.id)
        if(ids.length > 0){
          // console.log(ids)
          this.ids = ids
          this.open = true
        }else{
          this.$modal.msgError('当前选择的单据已审核！')
        }
      }else{
        this.$modal.msgError('请选择要审核的单据！')
      }
    },
    agreeApprove(){
      const ids = JSON.parse(JSON.stringify(this.ids))
      // console.log(ids)
      // console.log(this.approveRemark)
      planAllApprove(ids, 1, this.approveRemark).then(res => {
        // console.log(res)
        if(res.code == 200){
          this.getList()
          this.open = false
          this.$modal.msgSuccess("操作成功")
        }
      })
    },
    disagreeApprove(){
      const ids = JSON.parse(JSON.stringify(this.ids))
      planAllApprove(ids, 3, this.approveRemark).then(res => {
        if(res.code == 200){
          this.getList()
          this.$modal.msgSuccess("操作成功")
        }
      })
    },
    // 多选框选中数据
    handleSelectionChange(row) {
      if(row.length > 0){
        this.isApprove = true
      }else{
        this.isApprove = false
      }
      this.ids = row.map(item => item)
    },
    // 查看详情
    handleView(row) {
      this.$router.push({path:"/production/plan/detail",query:{id: row.id}})
    },
    // 删除
    handleDelete(row){
      this.$modal.confirm('是否确认删除？').then(() => {
        planDelete(row.id).then(() => {
          this.getList()
          this.$modal.msgSuccess("删除成功")
        })
      }).catch(() => {})
    },
  }
}
</script>
